(function(KKBus){

    function BusController() {
        var self = this;
        
        self.bus = ko.observableArray();
        self.coursesKat = ko.observableArray();
        self.coursesKat.sort(self.sort);
        
        self.getBus = function (page)
        {
           self.bus.removeAll();
            
            app.request++;
        
             var url = 'rest/busses/';
                               
           
        
        
        KKBus.Ajax.doGet(url, function(data, textStatus, jqXHR)
        {
      
        var jsData = JSON.parse(data);
        var i = jsData.length;
        
             {
	
	
}
            while(i--){
                self.bus.push(new KKBus.BusModel(jsData[i].id,jsData[i].brand, jsData[i].registartion , jsData[i].parking, jsData[i].description, jsData[i].places,jsData[i].year));          }
         app.request--;
                  
          if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " + BusController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
              });
         };
        
        
         self.deleteBus = function(id, index) {
         
         self.bus.remove(self.bus()[index]);    
             
         KKBus.Ajax.doDelete('rest/bus/'+id, function(result, textStatus, jqXHR) {
            });   
         };    
    
       self.validation = ko.observable(false);
       self.errorMessage = ko.observable("Blad");
       self.success = ko.observable(false);
    
       self.addBus = function() {
       
            var bus = JSON.stringify({
                     brand: $('#formBrand').val(),
                     registartion: $('#formRegistration').val(),
                     parking: $('#formParking').val(),
                     description: "Opis",
                     places: 40,
                     year: 2013                });
                
             if(KKBus.Validation.length($('#formBrand').val())
                    && KKBus.Validation.length($('#formRegistration').val())
                    && KKBus.Validation.length($('#formParking').val())){ 
                
                    if(KKBus.Validation.word($('#formBrand').val())
                       && KKBus.Validation.length($('#formParking').val())){
             
                        if(KKBus.Validation.rej($('#formRegistration').val())){
             
                             KKBus.Ajax.doPost('rest/bus', bus, function(result, textStatus, jqXHR) {
                                 if (result == "ADDED") {
                                 
                                        $('#formBrand').val(' ');
                                        $('#formRegistration').val(' ');
                                        $('#formParking').val(' ');
                                        self.success(true);
                                        setTimeout(function() {
                                                            
                                            
                                              self.success(false);
                                           }, 3000);
                                        
                                         var temp = JSON.parse(bus);
                                                                    
                                          var BusAdd = new KKBus.BusModel(36,temp.brand,temp.registartion,temp.parking,temp.description);

                                          self.bus.push(BusAdd); 
                                 
                                 }
                                });
                                
                                }else{
                                  
                                  self.validation(true);
                                  self.errorMessage("Bledny format numeru rejestracyjnego");
                                  setTimeout(function() { self.validation(false); }, 3000);
                             
                                };
                        
                           }else{
                           
                           self.validation(true);
                           self.errorMessage("Bledny format wprowadzonych danych!");
                           setTimeout(function() { self.validation(false); }, 3000);
                           
                          };
                       
                    }else{
                        
                      self.validation(true);
                      self.errorMessage("Puste pola!");
                      setTimeout(function() { self.validation(false); }, 3000);
                        
                };
         
    };


        
        self.viewCourses0 = ko.observable(false);
        self.viewCourses1 = ko.observable(false);
        self.viewCourses2 = ko.observable(false);
        
          self.showCourses = function() {
            
            if($('#formOpcja').val() == 0){
                self.viewCourses0(true);
                self.viewCourses1(false);
                self.viewCourses2(false);
                //setTimeout(function() { self.viewCourses0(false); }, 5000);             
                
                
            } else if ($('#formOpcja').val() == 1){
                self.viewCourses0(false);
                self.viewCourses1(true);
                self.viewCourses2(false);
                              
             
            }
            else {
                   
                self.viewCourses0(false);
                self.viewCourses1(false);
                self.viewCourses2(true);
            }
            
            
        };
        
        
    }
    KKBus. BusController =  BusController;
    
}(window.KKBus));

